/*
 * author      : 吴中勤 
 * date        : 2018/9/3
 * description : TODO
 */

package com.deepexi.adapter.sdk.moby;

import com.deepexi.adapter.common.CommonUtils;
import com.deepexi.adapter.common.HttpClientUtil;
import com.deepexi.adapter.common.sdk.RequestInfo;
import com.deepexi.adapter.sdk.moby.request.*;
import com.deepexi.adapter.sdk.moby.response.BaseMobyResponse;
import org.json.JSONException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.List;

public class MobySdk {

    private static final Logger logger = LoggerFactory.getLogger(MobySdk.class);
    private MobySdkConfig sdkConfig;

    public MobySdk(String profile) {
        if (!"prod".equals(profile)) {
            logger.warn("非生产环境: sdk config, profile=" + profile);
        }
        this.sdkConfig = MobySdkConfig.fromResource(profile);
    }

    /**
     * moby.sdk.host.car-center
     * moby.sdk.host.product-center
     */
    public void setConfigMobyHost(String mobyCarCenter, String mobyProductCenter) {
        this.sdkConfig.setMobyHost(mobyCarCenter, mobyProductCenter);

        logger.info("sdkConfig {}", sdkConfig.beautify());
    }

    public MobySdk() {
        this("prod");
    }

    /** 发送 http请求 */
    public String doHttp(RequestInfo requestInfo) throws IOException {
        return HttpClientUtil.postJson4StringBody(requestInfo.url, requestInfo.strBody);
    }


    /** 解析结果 */
    public <T> T parseResult(RequestInfo requestInfo, String result) throws JSONException {

        if (null == requestInfo) {
            throw new RuntimeException("requestInfo null");
        }

        MobyApiType mobyApiType = MobyApiType.fromName(requestInfo.apiType);

        // parse string to (T)object
        switch (mobyApiType) {
            case ExteriorPart:
                return (T) ResponseParser.parseResult(result);
            case InteriorPart:
                return (T) ResponseParser.parseResult(result);
            case Material:
                return (T) ResponseParser.parseResult(result);
            case VehicleInfo:
                return (T) ResponseParser.parseResult(result);
            case VehicleModel:
                return (T) ResponseParser.parseResult(result);
            case VehicleModelLine:
                return (T) ResponseParser.parseResult(result);
            default:
                throw new RuntimeException("不支持的api类型:" + mobyApiType.name);
        }
    }


    /**
     * do http and parse string result to object
     *
     * @throws IOException   网络异常
     * @throws JSONException 解析异常
     */
    public <T> T doHttpAndParseResult(RequestInfo requestInfo) throws IOException, JSONException {
        String result = this.doHttp(requestInfo);
        return parseResult(requestInfo, result);
    }

    /** 生成 外饰 请求 */
    public RequestInfo requestInfo4ExteriorPart(List<ExteriorPart> body) {
        return new RequestInfo(
                "",
                sdkConfig.getUrlByApiType(MobyApiType.ExteriorPart),
                CommonUtils.list2String(body),
                BaseMobyResponse.class,
                MobyApiType.ExteriorPart.name);
    }

    /** 生成 内饰 请求 */
    public RequestInfo requestInfo4InteriorPart(List<InteriorPart> body) {
        return new RequestInfo(
                "",
                sdkConfig.getUrlByApiType(MobyApiType.InteriorPart),
                CommonUtils.list2String(body),
                BaseMobyResponse.class,
                MobyApiType.InteriorPart.name);
    }

    /** 生成 物料 请求 */
    public RequestInfo requestInfo4Material(List<Material> body) {
        return new RequestInfo(
                "",
                sdkConfig.getUrlByApiType(MobyApiType.Material),
                CommonUtils.list2String(body),
                BaseMobyResponse.class,
                MobyApiType.Material.name);
    }

    /** 生成 车辆信息 请求 */
    public RequestInfo requestInfo4VehicleInfo(List<VehicleInfo> body) {
        return new RequestInfo(
                "",
                sdkConfig.getUrlByApiType(MobyApiType.VehicleInfo),
                CommonUtils.list2String(body),
                BaseMobyResponse.class,
                MobyApiType.VehicleInfo.name);
    }


    /** 生成 车型 请求 */
    public RequestInfo requestInfo4VehicleModel(List<VehicleModel> body) {
        return new RequestInfo(
                "",
                sdkConfig.getUrlByApiType(MobyApiType.VehicleModel),
                CommonUtils.list2String(body),
                BaseMobyResponse.class,
                MobyApiType.VehicleModel.name);
    }


    /** 生成 车系 请求 */
    public RequestInfo requestInfo4VehicleModelLine(List<VehicleModelLine> body) {
        return new RequestInfo(
                "",
                sdkConfig.getUrlByApiType(MobyApiType.VehicleModelLine),
                CommonUtils.list2String(body),
                BaseMobyResponse.class,
                MobyApiType.VehicleModelLine.name);
    }

}
